/**
 * created by dqn 2018/3/14 19:21
 */

package main.service.impl;

import main.entity.Comment;
import main.repository.CommentRepository;
import main.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentRepository commentRepository;

    @Override
    public List<Comment> getCommentsBySongPk(int pk) {
        return commentRepository.getCommentsBySong(pk).orElse(new ArrayList<>());
    }

    @Override
    public Comment addComment(Comment comment) {
        return commentRepository.save(comment);
    }

    @Override
    public boolean delCommentByPk(int pk) {
        try {
            commentRepository.deleteById((long) pk);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean delCommentsBySongPk(int pk) {
        try {
            commentRepository.deleteCommentsBySong(pk);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<Comment> getCommentsByUser(String name) {
        return commentRepository.getCommentsByUser(name).orElse(new ArrayList<>());
    }
}
